package lg.minecraft.plugin.pokemonmenu.config;

import lg.minecraft.plugin.library.config.LGBaseConfig;
import lg.minecraft.plugin.pokemonmenu.LGPokemonMenu;
import lg.minecraft.plugin.pokemonmenu.util.pokemondata.StorageType;
import lombok.Getter;

import java.io.File;
import java.util.List;

/**
 * @author langle__
 * @version 1.0
 */
@Getter
public final class MainConfig extends LGBaseConfig {

    private boolean commandMain;
    private int moneyHeal;
    private boolean defaultOpenWildPokemon;
    private boolean defaultViewPlayerParty;
    private boolean defaultAutoHeal;
    private boolean defaultDisabledView;
    private long displayTime;
    private long displayCooldown;
    private boolean removeMainHand;
    private boolean pokemonCloud;
    private int dollIvs;
    private boolean dollTradeable;
    private int toItem;
    private int toPokemon;
    private float scaleMin;
    private float scaleMax;
    private boolean realTimeUpdate;

    private StorageType pokemonCloudStorage;
    private File pokemonDataFolder;

    private String pokemonCloudMySqlUrl;
    private String pokemonCloudMySqlUser;
    private String pokemonCloudMySqlPassword;

    public MainConfig() {
        super(LGPokemonMenu.getInstance(), "config");
        init();
    }

    @Override
    public void load() {
        super.load();

        commandMain = getBoolean("command.main");

        moneyHeal = getInt("money_heal");

        defaultOpenWildPokemon = getBoolean("default_enabled.open_wild_pokemon");
        defaultViewPlayerParty = getBoolean("default_enabled.view_player_party");
        defaultAutoHeal = getBoolean("default_enabled.auto_heal");
        defaultDisabledView = getBoolean("default_enabled.disabled_view");

        displayTime = getLong("display.time");
        displayCooldown = getLong("display.cooldown");

        removeMainHand = getBoolean("gui.remove-main-hand");

        dollIvs = getInt("doll.ivs");
        dollTradeable = getBoolean("doll.tradeable");
        toItem = getInt("doll.toItem");
        toPokemon = getInt("doll.toPokemon");

        pokemonCloud = getBoolean("doll.pokemon_cloud.enabled");
        String pokemonCloudStorageString = getString("doll.pokemon_cloud.storage", "File");
        pokemonCloudStorage = StorageType.getStorageType(pokemonCloudStorageString);
        if (pokemonCloudStorage == null) {
            LGPokemonMenu.getInstance().severe("Invalid pokemon cloud storage type.");
            pokemonCloudStorage = StorageType.FILE;
        }
        pokemonDataFolder = new File(getString("doll.pokemon_cloud.file.path", "plugins\\LGPokemonMenu\\pokemon_data"));

        pokemonCloudMySqlUrl = getString("doll.pokemon_cloud.mysql.url");
        pokemonCloudMySqlUser = getString("doll.pokemon_cloud.mysql.user");
        pokemonCloudMySqlPassword = getString("doll.pokemon_cloud.mysql.password");

        scaleMin = getFloat("edit.scale.min");
        scaleMax = getFloat("edit.scale.max");

        realTimeUpdate = getBoolean("inventor.real_time_update");
    }

    public long getDisplayTime() {
        return displayTime * 1000;
    }

    public float getFloat(String path) {
        return getFloat(path, 0.0F);
    }

    public float getFloat(String path, float def) {
        Object val = get(path);
        if (val instanceof Integer) {
            return ((Integer) val).floatValue();
        } else if (val instanceof Double) {
            return ((Double) val).floatValue();
        } else if (val instanceof String) {
            try {
                return Float.parseFloat((String) val);
            } catch (NumberFormatException ignored) {}
        }
        return def;
    }

}
